﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace eDoc
{
    /// <summary>
    /// Summary description for deviceactive
    /// </summary>
    public class deviceactive : IHttpHandler
    {
        private string str = string.Empty;
        private int type = 2;
        public void ProcessRequest(HttpContext context)
        {

            if (context.Request.QueryString["type"] != null && context.Request.QueryString["type"].ToString() != string.Empty)
            {
                type = Convert.ToInt32(context.Request.QueryString["type"]);
            }

            DataTable dt = new DataTable();
            dt = BO.Data.Report_Device_Active(type);
            str = GetData(dt);
            context.Response.ContentType = "application/json";
            context.Response.Write(str);
        }
        private string GetData(DataTable dt)
        {
            string result = string.Empty;
            string activeTime = "";
            string activeCount = "";
            string activeTimeTemp = "";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if(type==2)
                {
                    activeTimeTemp = Convert.ToDateTime(dt.Rows[i]["ActiveTime"]).ToString("dd/MM/yyyy");
                }
                else
                {
                    activeTimeTemp = dt.Rows[i]["ActiveTime"].ToString();
                }
                activeCount += dt.Rows[i]["CountActive"].ToString() + ",";
                activeTime += "\"" + activeTimeTemp + "\",";
            }

            activeTime = activeTime.TrimEnd(',');
            activeCount = activeCount.TrimEnd(',');

            result = "[[" + activeTime + "],[" + activeCount + "]]";

            return result;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}